/**   
* @Package com.app.open.sys.menu.web 
*/
package com.wlyy.bcwlw.sys.menu.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wlyy.bcwlw.base.BaseController;
import com.wlyy.bcwlw.sys.application.dao.ApplicationDAO;
import com.wlyy.bcwlw.sys.menu.dao.MenuDAO;
import com.wlyy.bcwlw.sys.menu.dao.ModuleDAO;
import com.wlyy.bcwlw.sys.menu.entity.MenuDTO;
import com.wlyy.bcwlw.sys.menu.entity.ModuleDTO;
import com.wlyy.bcwlw.sys.menu.service.ModuleService;


/**
 * @ClassName. SysModuleController
 * @Description. TODO
 * @author zhangyang
 * @date 2015年8月8日 下午10:42:50
 * @version V1.0
 */
@RemoteProxy
@Controller
@RequestMapping("/sys/module")
public class SysModuleController extends BaseController{
	@Autowired
	private ModuleDAO moduleDAO;
	@Autowired
	private MenuDAO menuDAO;
	@Autowired
	private ModuleService moduleService;
	@Autowired
	private ApplicationDAO applicationDAO;
	
	@RequestMapping("/checkNameISExist")
	public String checkNameISExist(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String name = get(request,"editMoudel_name");
		String appId = get(request,"appId");
		String id = get(request,"id");
		String str ="true";
		ModuleDTO moduleDTO = new ModuleDTO();
		moduleDTO.setAppId(appId);
		moduleDTO.setName(name);
		int count = moduleDAO.count(moduleDTO);
		if(count >0){
			str="false";
		}
		if(StringUtils.isNotEmpty(id)){
			moduleDTO = moduleDAO.findById(id);
			if(moduleDTO!=null&&moduleDTO.getName().equals(name)){
				str="true";
			}
		}
		
		
		
		sendMessage(response, str);
		return null;
	}
	
	
	
	public ModuleDTO getModuleInfoById(String moduleId){
		if(StringUtils.isEmpty(moduleId)){
			return null;
		}
		ModuleDTO module = moduleService.queryWithModuleNameById(moduleId);	
		return module;
	}
	
	public ModuleDTO moduleInfoSave(ModuleDTO moduleDTO){
		ModuleDTO newModuleDTO =moduleService.save(moduleDTO);
		return newModuleDTO;
	}
	
	public boolean deleteModuleById(String moduleId){
		if(StringUtils.isEmpty(moduleId)){
			return false;
		}
		MenuDTO menuDTO = new MenuDTO();
		menuDTO.setModuleId(moduleId);
		int count = menuDAO.count(menuDTO);
		if(count >0){
			menuDAO.remove(menuDTO);
		}
		moduleDAO.removeById(moduleId);
		return true;
	}

}
